package com.customization.unionpay.market.dao;

import com.alibaba.fastjson.JSONObject;
import com.customization.unionpay.market.util.FormUtil;
import weaver.backup.logging.Logger;
import weaver.backup.logging.LoggerFactory;
import weaver.conn.RecordSet;

import java.util.ArrayList;
import java.util.List;


public class ClearFundDetailDao {

    public Logger log = LoggerFactory.getLogger(ClearFundDetailDao.class);


    /**
     * 根据 【营销平台活动授权码】+【清算日期】+【业务类型】 的联合条件查询清算资金明细
     * @param yxpthdsqm
     * @param qsrq
     * @param ywlx
     * @return
     */
    public List<JSONObject> selectByUnionKey(String yxpthdsqm, String qsrq, String ywlx){
        StringBuilder sql = new StringBuilder();
        sql.append("select id from ").append(com.customization.unionpay.market.util.FormUtil.CLEAR_FUND_DETAIL)
                .append(" where yxpthdsqm = '").append(yxpthdsqm).append("' and ")
                .append("qsrq = '").append(qsrq).append("' and ")
                .append("ywlx = '").append(ywlx).append("'");
        RecordSet rs = new RecordSet();
        List<JSONObject> results = new ArrayList<>();
        try {
            rs.execute(sql.toString());
            while (rs.next()){
                JSONObject json = new JSONObject();
                json.put("id",rs.getInt("id"));
                results.add(json);
            }
            return results;
        }catch (Exception e){
            log.error("查询清算资金明细失败");
        }
        return null;
    }


    public void insert(JSONObject jsonObject){
         StringBuilder sql = new StringBuilder();
        sql.append("insert into ").append(FormUtil.CLEAR_FUND_DETAIL)
                .append("(yxpthdsqm,ACT_NM,ACT_TOTAL_AT,ywlx,zjly,ptqdrq,jbr,zbdw,qsrq,qsje,hdjsbs,sjly," +
                        "zt,modedatacreater,formmodeid)");
         if(jsonObject!=null){
             sql.append(" values ")
                     .append("(")
                     .append("'").append(jsonObject.getString("yxpthdsqm")).append("'").append(",")
                     .append("'").append(jsonObject.getString("ACT_NM")).append("'").append(",")
                     .append(jsonObject.getString("ACT_TOTAL_AT")).append(",")
                     .append("'").append(jsonObject.getString("ywlx")).append("'").append(",")
                     .append(jsonObject.getString("zjly")).append(",")
                     .append("'").append(jsonObject.getString("ptqdrq")).append("'").append(",")
                     .append("'").append(jsonObject.getString("jbr")).append("'").append(",")
                     .append("'").append(jsonObject.getString("zbdw")).append("'").append(",")
                     .append("'").append(jsonObject.getString("qsrq")).append("'").append(",")
                     .append(jsonObject.getString("qsje")).append(",")
                     .append("'").append(jsonObject.getString("hdjsbs")).append("'").append(",")
                     .append(jsonObject.getString("sjly")).append(",")
                     .append(jsonObject.getString("zt")).append(",")
                     .append(jsonObject.getString("modedatacreater")).append(",")
                     .append(jsonObject.getString("formmodeid"))
                     .append(")");
             RecordSet rs = new RecordSet();
             boolean result = rs.execute(sql.toString());
             if(!result){
                 log.error("清算资金明细台账数据插入失败"+jsonObject.toJSONString());
             }
         }

    }
}
